import datetime
import threading
from orm_info import CityInfoTest
from weather import *
from loguru import logger
import queue

def getWeatherInfo():
    logger.info("开始收集所有天气信息")
    rows = CityInfo.select()
    tempInfo = []
    for row in rows:
        r = get_weather_all(row.adcode)
        if r != False:
            onecityweather = process_weatherinfo(r)
            tempInfo += onecityweather
        else:
            pass
    logger.info("天气信息收集完成")
    return tempInfo

'''
#协程处理，效果不明显
async def syncGetWeather(adcode):
    r = get_weather_all(adcode)
    if r != False:
        return process_weatherinfo(r)
    else:
        return False

async def main():
    logger.info("开始收集所有天气信息")
    rows = CityInfo.select()
    tempInfo = []
    for i in rows:
        r = await syncGetWeather(i.adcode)
        if r != False:
            tempInfo += r
        else:
            logger.warning("跳过")
    logger.info("天气信息收集完成")
    return tempInfo
'''

#多进程处理，单次查询落库时间由17分钟左右提高到6线程297秒/8线程262秒
def work_weather(q):
    logger.info("task begin")
    start_time = datetime.datetime.now()
    while True:
        adcode = q.get()
        if adcode is None:
            break
        r = get_weather_all(adcode)
        if r != False:
            onecityweather = process_weatherinfo(r)
            WeatherInfo.insert_many(onecityweather).execute()
        logger.debug("Processing item:{}".format(adcode))
    end_time = datetime.datetime.now()
    execution_time = end_time - start_time
    logger.info("代码执行时间为：{}秒".format(execution_time.seconds))
    logger.info("task end")

def run_task():
    # 创建一个队列对象
    q = queue.Queue()
    # 启动多个工作线程
    for i in range(7):
        logger.debug("{}-processed working...".format(i))
        t = threading.Thread(target=work_weather, args=(q,))
        t.start()
    # 向队列中添加数据
    for row in CityInfo.select():
        q.put(row.adcode)
    # 等待所有工作线程完成
    for i in range(7):
        q.put(None)
    # 输出结果
    logger.info("All items processed")

if __name__ == '__main__':
    # r = threadMain()
    # logger.info("天气写入数据库：开始")
    # WeatherInfo.insert_many(r).execute()
    # logger.info("天气写入数据库：完成")
    run_task()



